2
2
.
.
2
2
.
.
2
2
S
S
c
c
a
a
l
l
a
a
r
r
s
s
I
I
n
n
f
f
o
o
Scalar Data Types can only store single value.
Scalar Data Types
TYPE
DESCRIPTION
SIZE
null
Represents undefined/unknown value.
boolean
Represents logical TRUE or FALSE.
byte
Represents integer number (20, -54).
8-bit two's complement
short
Represents integer number (20, -54).
16-bit two's complement
int
Represents integer number (20, -54).
32-bit two's complement
long
Represents integer number (20, -54).
64-bit two's complement
float
Represents real number (-45.87).
32-bit IEEE 754
double
Represents real number (-45.87).
64-bit IEEE 754
char
Represents Unicode character.
16-bit
n
n
u
u
l
l
l
l
Null data type can only have null value and can be created using Null Literal.
Null data type can only be assigned to reference type variable which are those that hold Classes and Strings.
You cannot assign null data to primitive variables like boolean, int, float, etc.
null
String name = null; //Create null data using null literal.
Integer age = null; //Assign null to Integer class. Different from int primitive.
int temp = null; //Error. Cannot assign null data to primitive variables.
b
b
o
o
o
o
l
l
e
e
a
a
n
n
Boolean data type can have only two possible values: true or false and can be created using Boolean Literal.
bool
boolean value = true; //Create boolean data using boolean literal.
boolean value = Boolean.getBoolean("false"); //Create boolean data using getBoolean() method.
b
b
y
y
t
t
e
e
Byte data type is type of data that represents integer number and can be created using Integer Literal.
Check out byte cheat sheet containing code samples for working with byte data type.
byte
byte value = 65; //Create byte data using long literal which is then implicitly converted to byte data.
s
s
h
h
o
o
r
r
t
t
Short data type is type of data that represents integer number and can be created using Integer Literal.
Check out short cheat sheet containing code samples for working with short data type.
short
short value = 65; //Create short data using integer literal which is implicitly converted to short data.
i
i
n
n
t
t
Int data type is type of data that represents integer number and can be created using Integer Literal.
Check out int cheat sheet containing code samples for working with int data type.
int
//CREATE INTEGER DATA USING INTEGER LITERAL IN DIFFERENT NOTATIONS.
int value = 65; //Decimal.
value = 0101; //Octal.
value = 0x41; //Hexadecimal.
//CREATE INTEGER DATA USING method parseInt().
value = Integer.parseInt("65" ); //Decimal since default base is 10.
value = Integer.parseInt("1000001", 2); //Binary since base is set to 2.
l
l
o
o
n
n
g
g
Long data type is type of data that represents integer number and can be created using Long Literal.
Check out long cheat sheet containing code samples for working with long data type.
Test.java
//CREATE INTEGER DATA USING INTEGER LITERAL IN DIFFERENT NOTATIONS.
long value = 65; //Decimal.
value = 0101; //Octal.
value = 0x41; //Hexadecimal.
//CREATE INTEGER DATA USING method parseInt().
value = Long.parseLong("65" ); //Decimal since default base is 10.
value = Long.parseLong("1000001", 2); //Binary since base is set to 2.
f
f
l
l
o
o
a
a
t
t
Float data type is type of data that represents real number and an be created using Float Literal.
Check out float cheat sheet containing code samples for working with float data type.
float
//CREATE FLOAT DATA USING FLOAT LITERAL IN DIFFERENT NOTATIONS.
float value = 65.23F; //Basic notation.
value = 0.6523E2F; //Scientific notation.
//CREATE FLOAT DATA USING METHOD parseFloat().
value = Float.parseFloat("65.23");
d
d
o
o
u
u
b
b
l
l
e
e
Double data type is type of data that represents real number and can be created using Double Literal.
Check out double cheat sheet containing code samples for working with double data type.
double
//CREATE DOUBLE DATA USING DOUBLE LITERAL IN DIFFERENT NOTATIONS.
double value = 65.23F; //Basic notation.
value = 0.6523E2F; //Scientific notation.
//CREATE DOUBLE DATA USING method parseFloat().
value = Double.parseDouble("65.23");
c
c
h
h
a
a
r
r
Char data type is type of data that uses integer number to represents single character based on ASCII Table.
Char data can be created using Character Literal in different notations.
Check out char cheat sheet containing code samples for working with char data type.
char
//CREATE CHAR DATA.
char letter = 'A'; //Create char data using character literal.
letter = 65; //Create char data using integer literal which is implicitly converted to char data.